// API配置文件
export const API_CONFIG = {
    // 开发环境
    development: {
        warehouseApiUrl: 'http://localhost:5000/',
        readApiUrl: 'http://localhost:5062/',
        writeApiUrl: 'http://localhost:5251/',
        customerApiUrl: 'http://localhost:5000/',
        salesOrderApiUrl: 'http://localhost:5000/',
        description: '开发环境 - 本地后端服务'
    },

    // 测试环境
    test: {
        warehouseApiUrl: 'http://localhost:5000/',
        readApiUrl: 'http://localhost:5062/',
        writeApiUrl: 'http://localhost:5251/',
        customerApiUrl: 'http://localhost:5000/',
        salesOrderApiUrl: 'http://localhost:5000/',
        description: '测试环境 - 本地后端服务'
    },

    // 生产环境
    production: {
        warehouseApiUrl: 'http://localhost:5000/',
        readApiUrl: 'http://localhost:5062/',
        writeApiUrl: 'http://localhost:5251/',
        customerApiUrl: 'http://localhost:5000/',
        salesOrderApiUrl: 'http://localhost:5000/',
        description: '生产环境 - 生产后端服务'
    }
}

// 获取当前环境配置
export function getCurrentApiConfig() {
    const mode = import.meta.env.MODE || 'development'
    return API_CONFIG[mode] || API_CONFIG.development
}

// 获取仓库管理API地址
export function getWarehouseApiUrl() {
    return getCurrentApiConfig().warehouseApiUrl
}

// 获取读取API地址
export function getReadApiUrl() {
    return getCurrentApiConfig().readApiUrl
}

// 获取写入API地址
export function getWriteApiUrl() {
    return getCurrentApiConfig().writeApiUrl
}

// 获取客户管理API地址
export function getCustomerApiUrl() {
    return getCurrentApiConfig().customerApiUrl
}

// 获取采购订单API地址
export function getPurchaseOrderApiUrl() {
    return getCurrentApiConfig().warehouseApiUrl
}

// 获取销售订单API地址
export function getSalesOrderApiUrl() {
    return getCurrentApiConfig().salesOrderApiUrl
}

// 获取所有API地址用于测试
export function getAllApiUrls() {
    const config = getCurrentApiConfig()
    return [
        {
            name: '仓库管理API',
            url: config.warehouseApiUrl,
            description: '供应商、仓库等管理功能'
        },
        {
            name: '读取API',
            url: config.readApiUrl,
            description: '用户、角色、权限等读取功能'
        },
        {
            name: '写入API',
            url: config.writeApiUrl,
            description: '用户、角色、权限等写入功能'
        },
        {
            name: '客户管理API',
            url: config.customerApiUrl,
            description: '客户信息管理功能'
        }
    ]
}

// 测试所有API连接
export async function testAllApiConnections() {
    const { testApiConnection } = await import('@/utils/apiConnectionTest')
    const urls = getAllApiUrls()
    const results = []

    for (const apiInfo of urls) {
        try {
            const result = await testApiConnection(apiInfo.url)
            results.push({
                ...apiInfo,
                ...result
            })
        } catch (error) {
            results.push({
                ...apiInfo,
                success: false,
                error: error.message
            })
        }
    }

    return {
        success: results.every(r => r.success),
        results,
        summary: {
            total: results.length,
            success: results.filter(r => r.success).length,
            failed: results.filter(r => !r.success).length
        }
    }
} 